var flagName = false;
var oName = document.getElementById("name");
var oTname = document.getElementById("tname");
oName.onblur = function() {
	var regName = /^[\u4e00-\u9fa5]{2,4}$/;
	$.post("./php/NewUser.php", {
		userName: oName.value
	}).then(function(res) {
		if (res == "0") {
			oTname.innerHTML = "*用户名已经存在或格式有误";
			flagName = false;
		} else if (res == "1" && regName.test(oName.value)) {
			oTname.innerHTML = "*用户名可用";
			flagName = true;
		}
	});
}

var flagPass = false;
var oPassword = document.getElementById("password");
var oTpassword = document.getElementById("tpassword");
oPassword.onblur = function() {
	var regName = /^.{6}$/;
	if (regName.test(oPassword.value)) {
		flagPass = true;
		oTpassword.innerHTML = "*格式正确";
	} else {
		flagPass = false;
		oTpassword.innerHTML = "*由字母、数字、下划线组成，不能少于6位";
	}
}

var flagNum = false;
var oNum = document.getElementById("num");
var oTnum = document.getElementById("tnum");
oNum.onblur = function() {
	var regName =  /^(13|15|18)\d{9}$/;
	if (regName.test(oNum.value)) {
		oTnum.innerHTML = "*格式正确";
		flagNum = true;
	} else {
		oTnum.innerHTML = "*请输入以13，15，18开头11位手机号";
		flagNum = false;
	}
}


var oMyForm = document.getElementById("button");
oMyForm.onclick = function() {
	if (flagName == true&& flagPass ==true && flagNum == true) {
		$.post("./php/nuser.php", {
			username: oName.value,
			userpass:oPassword.value,
			usernum:oNum.value
		}).then(function(res) {
			location.href="./login.html";
		});
	}else{
		if (oName.value=="") {
			oTname.innerHTML = "*用户名必填";
		}
		if (oPassword.value=="") {
			oTpassword.innerHTML = "*密码必填";
		}
		if (oNum.value=="") {
			oTnum.innerHTML = "*手机号必填";
		}
       return false; 
	}
	return false; 
}
